package com.besttone.rabbit.producer;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.GenericMessageConverter;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

	@Bean
	public RabbitMessagingTemplate msgMessageTemplate(ConnectionFactory connectionFactory) {
		RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
		// 参数列表分别是：1.交换器名称(default.topic 为默认值),2.是否长期有效,3.如果服务器在不再使用时自动删除交换器
		TopicExchange exchange = new TopicExchange("default.topic", true, false);
		rabbitAdmin.declareExchange(exchange);
		// 1.队列名称,2.声明一个持久队列,3.声明一个独立队列,4.如果服务器在不再使用时自动删除队列
		Queue queue = new Queue("test.demo.send", true, false, false);
		rabbitAdmin.declareQueue(queue);
		// 1.queue:绑定的队列,2.exchange:绑定到那个交换器,3.test2.send:绑定的路由名称
		rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test2.send"));
		return simpleMessageTemplate(connectionFactory);
	}

	public RabbitMessagingTemplate simpleMessageTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate template = new RabbitTemplate(connectionFactory);
		RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
		rabbitMessagingTemplate.setMessageConverter(new GenericMessageConverter());
		rabbitMessagingTemplate.setRabbitTemplate(template);
		return rabbitMessagingTemplate;
	}

	/**
	 * 注入配置文件属性
	 */
	@Value("${spring.rabbitmq.addresses}")
	String addresses;// MQ地址
	@Value("${spring.rabbitmq.username}")
	String username;// MQ登录名
	@Value("${spring.rabbitmq.password}")
	String password;// MQ登录密码
	@Value("${spring.rabbitmq.virtual-host}")
	String vHost;// MQ的虚拟主机名

	/**
	 * 创建 ConnectionFactory
	 *
	 * @return
	 * @throws Exception
	 */
	@Bean
	public ConnectionFactory connectionFactory() throws Exception {
		return connectionFactory(addresses, username, password, vHost);
	}

	public ConnectionFactory connectionFactory(String addresses, String username, String password, String vHost)
			throws Exception {
		CachingConnectionFactory factoryBean = new CachingConnectionFactory();
		factoryBean.setVirtualHost(vHost);
		factoryBean.setAddresses(addresses);
		factoryBean.setUsername(username);
		factoryBean.setPassword(password);
		return factoryBean;
	}
}
